package com.wswy.wyjk.model

import android.support.v4.text.HtmlCompat
import com.wswy.wyjk.model.dto.PracticeDto

class PracticeData(private val practiceDto: PracticeDto) {

    companion object {

        const val TYPE_JUDGE = 1

        const val TYPE_RADIO = 2

        const val TYPE_MULTIPLE = 3
    }

    val id: Int = practiceDto.questionId

    var doIt: Int = 0

    var myAnswer: String = ""

    val chapterId = practiceDto.chapterId

    val label = practiceDto.label

    val question = practiceDto.question


    val answer = practiceDto.answer

    val optionA = practiceDto.optionA

    val optionB = practiceDto.optionB

    val optionC = practiceDto.optionC

    val optionD = practiceDto.optionD

    val mediaUrl = practiceDto.mediaUrl

    val difficulty = practiceDto.difficulty

    var isDo = practiceDto.isDo

    var doA=practiceDto.doA

    /**
     * 题目类型 判断题 单选题 多选题
     */
    val optionType = practiceDto.optionType

    val explain by lazy {
        HtmlCompat.fromHtml(practiceDto.explain, HtmlCompat.FROM_HTML_MODE_COMPACT)
    }

    val conciseExplain by lazy {
        practiceDto.conciseExplain
    }

    /**
     * 解析答案描述
     */
    val answerStr by lazy {

        if (isMultipleSelection()) {
            //多选题
            val sb = StringBuilder()
            for (i in 0..3) {
                val stBit = (1 shl (i + 4) and answer)
                if (stBit != 0) {
                    //选中了

                    sb.append(
                        when (i) {
                            0 -> "A"
                            1 -> "B"
                            2 -> "C"
                            3 -> "D"
                            else -> ""
                        }
                    )
                }
            }

            sb.toString()

        } else {
            //单选
            var option = -1
            for (i in 0..3) {
                val stBit = (1 shl (i + 4) and answer)

                if (stBit != 0) {
                    //选中了
                    option = i
                    break
                }
            }

            when (option) {
                0 -> "A"
                1 -> "B"
                2 -> "C"
                3 -> "D"
                else -> ""
            }
        }
    }

    override fun toString(): String {
        return "PracticeData(id=$id, label='$label', question='$question', answer=$answer, optionA='$optionA', optionB='$optionB', optionC='$optionC', optionD='$optionD')"
    }


    fun isMultipleSelection() = optionType == TYPE_MULTIPLE

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as PracticeData

        if (id != other.id) return false
        if (chapterId != other.chapterId) return false

        return true
    }

    override fun hashCode(): Int {
        var result = id
        result = 31 * result + chapterId
        return result
    }

}